<?php

class database {

    private $host;
    private $user;
    private $pass;
    private $name;
    
    function __construct(){
        //databse variables
        $this->host = "localhost";
        $this->user = "root";
        $this->pass = "";
        $this->name = "database";
        
        //connect to database
        $link = mysql_connect($this->host, $this->user, $this->pass);
        if(!$link){
            die('Could not connect: ' . mysql_error());
        }
        //select database
        if(!mysql_select_db("database")){
            die('Could not select database: ' . mysql_error());
        }
    }

    public function select($table, $fields = "*", $where = NULL){
        //query
        $q = isset($where) ? "SELECT $fields FROM $table WHERE $where" : "SELECT $fields FROM $table";
        $result = mysql_query($q);
        
        //check rows
        $rows = mysql_num_rows($result);
        if($rows){
            return $result; 
        } else {
            // return nothing
            return null;
        }
    } 

}

// example usage
// $db->select can accept 1, 2, or 3 arguments

echo '<h1>Database Test</h1>';

if($db = new database()){
  
    //select from database
    if($names = $db->select("data")){
        while($row = mysql_fetch_array($names)){
            echo $row['value'] . '<br />';
        }
    } else {
        // throw error
        echo "User not found";
    }
    
    
} else {
    echo 'Database Error';
}


?>
